package org.sky.sys.controller;

import org.apache.logging.log4j.Logger;
import org.sky.exception.ServiceException;
import org.sky.sys.entity.SysUser;
import org.sky.sys.entity.SysWidget;
import org.sky.sys.service.SysService;
import org.sky.sys.service.SysWidgetService;
import org.sky.sys.util.BspUtils;
import org.sky.sys.util.MenuStru;
import org.sky.util.JsonUtils;
import org.sky.util.LogUtils;
import org.sky.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author weifx
 * @Title: SysController
 * @ProjectName springboot-platform
 * @Description: TODO系统控制层
 * @date 2019/2/10 21:22
 * @Version 1.0
 */
@Controller
public class SysController {
    private static final Logger log = LogUtils.getLogger(SysController.class);
    @Autowired
    private SysService sysService;
    @Autowired
    private SessionRegistry sessionRegistry;
    @Autowired
    private SysWidgetService sysWidgetService;

    /**
     * 首页
     * @return
     */
    @GetMapping("/")
    public String index(){
        return "login";
    }

    /**
     * 登录页
     * @return
     */
    @GetMapping("/login")
    public String login(){
        return "login";
    }

    /**
     * 单点登录
     * @return
     */
    @GetMapping("/singleLogin/{username}/{password}")
    public ModelAndView singleLogin(
            @PathVariable String username,
            @PathVariable String password){
        ModelAndView mv = new ModelAndView();
        mv.addObject("username",username);
        mv.addObject("password",password);
        mv.setViewName("singleLogin");
        return mv;
    }

    /**
     * 登录成功请求
     * @return
     */
    @GetMapping("/loginSuccess")
    public ModelAndView loginSuccess(){
        ModelAndView mv = new ModelAndView();
        SysUser loginuser = BspUtils.getLoginUser();
        int onlineNo = sessionRegistry.getAllPrincipals().size();
        String msg = "欢迎【"+loginuser.getName()+"】登录	当前"+onlineNo+"用户在线";
        mv.addObject("LoginUserMsg",msg);
        mv.setViewName("main/main");
        log.info(msg);
        return mv;
    }

    /**
     * 查询菜单
     * @return
     */
    @PostMapping(value = "/sys/getNavMenu",produces = "text/html;charset=UTF-8")
    public @ResponseBody String getNavMenu(){
        List<MenuStru> list = sysService.getLeftNavMenuData();
        return JsonUtils.obj2json(list);
    }
    /**
     * 工作台
     * @return
     */
    @GetMapping(value = "/sys/workbench")
    public ModelAndView workbench() {
        String userCode=BspUtils.getLoginUser().getCode();
        List<SysWidget> list = sysWidgetService.selectSysWidgetListByUserCode(userCode);
        ModelAndView mv =  new ModelAndView();
        mv.addObject("widgetList", JsonUtils.obj2json(list));
        mv.setViewName("main/workbench");
        return mv;
    }
    /**
     * 打开修改密码页面
     * @return
     */
    @GetMapping(value = "/sys/initUpdatePwdPage")
    public String initUpdatePwdPage(){
        return "main/updatepwd";
    }

    /**
     *  修改密码
     * @param request
     * @return
     */
    @PostMapping(value = "/sys/saveUpdatePwd",produces = "text/html;charset=UTF-8")
    public @ResponseBody String saveUpdatePwd(HttpServletRequest request){
        String password = request.getParameter("password");
        String password1 = request.getParameter("password1");
        String password2 = request.getParameter("password2");
        Result result = null;
        try {
            sysService.saveUpdatePwd(password, password1, password2);
            result = Result.success("修改成功");
        }catch (ServiceException e){
            result = Result.error("修改失败"+e.getMessage(),e.getMessage());
        }
        return JsonUtils.obj2json(result);
    }
}
